package com.hscm.boss;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.hscm.boss.dao.entity.DicPayType;
import com.hscm.boss.dao.mapper.DicPayTypeMapper;
import com.hscm.boss.dao.mapper.DicPayWayMapper;
import com.hscm.boss.dao.result.DicEntity;
import com.hscm.boss.dao.result.DicPayTypeGroup;
import jakarta.annotation.Resource;
import org.ehcache.xml.model.ThreadPoolsType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;


@EnableAsync
@MapperScan("com/hscm/boss/dao/mapper")
@SpringBootApplication
public class PowerBossApplication implements CommandLineRunner {

    //创建线程池对象 并将线程池放到容器中 （这里是将生成对excel账单功能用多线程执行）
    @Bean
    public ThreadPoolExecutor poolExecutor(){
        return new ThreadPoolExecutor(13, 20, 10, TimeUnit.MINUTES,new LinkedBlockingQueue<>());
    }


    //支付方式的字典list
    public static List<DicEntity> PAYWAY_DIC_LIST = new ArrayList<>();


    //支付方式的Map
    public static Map<String, String> PAYWAY_DIC_MAP = new HashMap<>();

    //支付类型
    public static Map<String, List<DicEntity>> TYPE_DIC_MAP = new HashMap<>();

    //支付类型的名称的Map
    public static Map<String, String> TYPE_NAME_MAP = new HashMap<>();

    //添加分页插件
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
        return interceptor;
    }

    public static void main(String[] args) {
        SpringApplication.run(PowerBossApplication.class, args);
    }


    //注入Spring容器对象
    @Resource
    private ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {
        DicPayWayMapper payWayMapper = applicationContext.getBean(DicPayWayMapper.class);
        PAYWAY_DIC_LIST = payWayMapper.selectPayWayDicList();

        /* list ===> map     实现将在内存中含有这个Map的集合 方便读取*/
        PAYWAY_DIC_MAP = PAYWAY_DIC_LIST.stream()
                .collect(Collectors.toMap(DicEntity::getCode, DicEntity::getLabel));

        //支付类型的的Map数据
        DicPayTypeMapper dicPayTypeMapper = applicationContext.getBean(DicPayTypeMapper.class);
        List<DicPayTypeGroup> groupList = dicPayTypeMapper.selectPayTypeGroupContext();

        groupList.forEach(dicPayTypeGroup -> {
            ArrayList<DicEntity> dicPayTypeArrayList = new ArrayList<>();
            String payWayCode = dicPayTypeGroup.getPayWayCode();
            String typeCode = "";
            String typeName = "";
            String content = dicPayTypeGroup.getContent();
            for (String cet : content.split(";")) {
                typeCode = cet.split("#")[0];
                typeName = cet.split("#")[1];
                dicPayTypeArrayList.add(new DicEntity(typeName, typeCode));
                TYPE_NAME_MAP.put(payWayCode + ":" + typeCode, typeName);
            }

            TYPE_DIC_MAP.put(payWayCode, dicPayTypeArrayList);
        });

        System.out.println(TYPE_NAME_MAP);
    }
}
